<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>力扣-92-反转链表II | 园田家</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="力扣：92.反转链表II先让我秀一波提交记录图。因为这是我第一次靠自己想出来并且通过的第一道难度中等题。芜湖！    题目描述：给你单链表的头指针 head 和两个整数left 和 right ，其中left &lt;&#x3D; right 。请你反转从位置 left 到位置 right 的链表节点，返回 反转后的链表 。   示例1：   12输入：head &#x3D; [1,2,3,4,5], le">
<meta property="og:type" content="article">
<meta property="og:title" content="力扣-92-反转链表II">
<meta property="og:url" content="http://example.com/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/index.html">
<meta property="og:site_name" content="园田家">
<meta property="og:description" content="力扣：92.反转链表II先让我秀一波提交记录图。因为这是我第一次靠自己想出来并且通过的第一道难度中等题。芜湖！    题目描述：给你单链表的头指针 head 和两个整数left 和 right ，其中left &lt;&#x3D; right 。请你反转从位置 left 到位置 right 的链表节点，返回 反转后的链表 。   示例1：   12输入：head &#x3D; [1,2,3,4,5], le">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/images/leetcode-92/img1.jpg">
<meta property="og:image" content="http://example.com/images/leetcode-92/img2.jpg">
<meta property="article:published_time" content="2021-03-18T15:46:12.000Z">
<meta property="article:modified_time" content="2021-03-18T16:28:02.400Z">
<meta property="article:author" content="kurodasense">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/images/leetcode-92/img1.jpg">
  
    <link rel="alternate" href="/atom.xml" title="园田家" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">园田家</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-力扣-92-反转链表II" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/" class="article-date">
  <time datetime="2021-03-18T15:46:12.000Z" itemprop="datePublished">2021-03-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      力扣-92-反转链表II
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="力扣：92-反转链表II"><a href="#力扣：92-反转链表II" class="headerlink" title="力扣：92.反转链表II"></a>力扣：92.反转链表II</h3><p><img src="/images/leetcode-92/img1.jpg" alt="提交记录"><br>先让我秀一波提交记录图。因为这是我第一次靠自己想出来并且通过的第一道难度中等题。芜湖！  </p>
<hr>
<h3 id="题目描述："><a href="#题目描述：" class="headerlink" title="题目描述："></a>题目描述：</h3><p>给你单链表的头指针 <code>head</code> 和两个整数<code>left</code> 和 <code>right</code> ，其中<code>left &lt;= right</code> 。<br>请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点，返回 <strong>反转后的链表</strong> 。  </p>
<h5 id="示例1："><a href="#示例1：" class="headerlink" title="示例1："></a>示例1：</h5><p><img src="/images/leetcode-92/img2.jpg" alt="示例1">  </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入：head &#x3D; [1,2,3,4,5], left &#x3D; 2, right &#x3D; 4</span><br><span class="line">输出：[1,4,3,2,5]</span><br></pre></td></tr></table></figure>
<h5 id="示例2："><a href="#示例2：" class="headerlink" title="示例2："></a>示例2：</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">输入：head &#x3D; [5], left &#x3D; 1, right &#x3D; 1</span><br><span class="line">输出：[5]</span><br></pre></td></tr></table></figure>
<h5 id="提示："><a href="#提示：" class="headerlink" title="提示："></a>提示：</h5><ul>
<li>链表中节点数目为<code>n</code></li>
<li><code>1 &lt;= n &lt;= 500</code></li>
<li><code>-500 &lt;= Node.val &lt;= 500</code></li>
<li><code>1 &lt;= left &lt;= right &lt;= n</code>  </li>
</ul>
<hr>
<h5 id="进阶：你可以使用一趟扫描完成反转吗？"><a href="#进阶：你可以使用一趟扫描完成反转吗？" class="headerlink" title="进阶：你可以使用一趟扫描完成反转吗？"></a>进阶：你可以使用一趟扫描完成反转吗？</h5><p>我的题解：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for singly-linked list.</span></span><br><span class="line"><span class="comment"> * public class ListNode &#123;</span></span><br><span class="line"><span class="comment"> *     int val;</span></span><br><span class="line"><span class="comment"> *     ListNode next;</span></span><br><span class="line"><span class="comment"> *     ListNode() &#123;&#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int val) &#123; this.val = val; &#125;</span></span><br><span class="line"><span class="comment"> *     ListNode(int val, ListNode next) &#123; this.val = val; this.next = next; &#125;</span></span><br><span class="line"><span class="comment"> * &#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> ListNode <span class="title">reverseBetween</span><span class="params">(ListNode head, <span class="keyword">int</span> left, <span class="keyword">int</span> right)</span> </span>&#123;</span><br><span class="line">        ListNode next = head;</span><br><span class="line">        <span class="keyword">boolean</span> flag = <span class="keyword">false</span>;</span><br><span class="line">        Stack&lt;Integer&gt; st = <span class="keyword">new</span> Stack&lt;Integer&gt;();</span><br><span class="line">        <span class="keyword">int</span> count = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span>(count &lt;= right)&#123;</span><br><span class="line">            <span class="keyword">if</span>(count == left)&#123;</span><br><span class="line">                flag = <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(flag)&#123;</span><br><span class="line">                st.push(next.val);</span><br><span class="line">            &#125;</span><br><span class="line">            next = next.next;</span><br><span class="line">            count++;</span><br><span class="line">        &#125;</span><br><span class="line">        next = head;</span><br><span class="line">        flag = <span class="keyword">false</span>;</span><br><span class="line">        count = <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">while</span>(count &lt;= right)&#123;</span><br><span class="line">            <span class="keyword">if</span>(count == left)&#123;</span><br><span class="line">                flag = <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(flag)&#123;</span><br><span class="line">                next.val = st.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            next = next.next;</span><br><span class="line">            count++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> head;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>看示例1可知，输入是一个头指针<code>head</code>、反转的起始位置<code>left</code>和结束位置<code>right</code>。要反转数字的话，<br>那就肯定要记录反转前的数字嘛。所以我就想到了用栈去做。<br>我们定义一个栈<code>Stack&lt;Integer&gt; st = new Stack&lt;Integer&gt;()</code>，并且在第一次循环中，将从<code>left</code>到<br><code>right</code>的数字都押入到栈中。那示例1作为例子，第一次循环结束后，栈内应该是这样的：<br><code>stack = [2, 3, 4]</code>。<br>显然，当栈<code>stack</code>执行<code>pop()</code>操作的话，是把顶部的元素弹出的。所以，我们把<code>stack</code>全部<code>pop()</code>完的话，<br>元素的弹出顺序就应该是<code>[4, 3, 2]</code>。<br>看！这是不是就是反转后的链表！<br>所以，我们只要再进行第二次循环，把从<code>left</code>到<code>right</code>位置的值用<code>stack.pop()</code>重新赋值就可以完成<br>链表的反转了。<br>还有一点，因为<code>left</code>和<code>right</code>给的值的意思是链表的位置。比如，<code>left = 2</code>和<code>right = 4</code>，是指从链表的第2个位置<br>到第4个位置之间的3个结点反转。所以我就设了一个<code>count</code>来计数得出目前所在的位置。（一开始我没注意到这个问题，<br>弄得我两次都提交出错了，，，）</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/" data-id="ckmf1s8w50000e0v5ecv7cbxw" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/03/21/hasNext-%E4%B8%8EhasNextLine/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          hasNext()与hasNextLine()
        
      </div>
    </a>
  
  
    <a href="/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">力扣-115-不同的子序列</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">十一月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/04/18/leetcode-26/">leetcode-26</a>
          </li>
        
          <li>
            <a href="/2021/03/21/hasNext-%E4%B8%8EhasNextLine/">hasNext()与hasNextLine()</a>
          </li>
        
          <li>
            <a href="/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/">力扣-92-反转链表II</a>
          </li>
        
          <li>
            <a href="/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/">力扣-115-不同的子序列</a>
          </li>
        
          <li>
            <a href="/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/">力扣: 54.螺旋矩阵</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 kurodasense<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>